package me.zk.practice1.hw;

import java.util.Scanner;

/**
 * @author zhangkai
 * @date 2023/8/8 10:09
 */
public class HWBJ65 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String target = scanner.nextLine();

        String source = scanner.nextLine();

        int index = source.length();
        int n = 0;
        for (int i = target.length() - 1; i >= 0; i--) {
            for (int j = index-1; j >= 0; j--) {
                if (target.charAt(i) == source.charAt(j)) {
                    n++;
                    index = j;
                    break;
                }
            }


        }
        if (n == target.length()) {
            System.out.println(index);
        } else {
            System.out.println(-1);
        }
    }
}
